/*
*MainFlvPlayer
*chenjie
*2012-5-3
*/
package com.player
{
	import com.data.Data;
	import com.data.PlayModel;
	import com.event.PlayerEvent;
	
	import fl.video.AutoLayoutEvent;
	import fl.video.FLVPlayback;
	import fl.video.VideoEvent;
	import fl.video.VideoScaleMode;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.Capabilities;
	import flash.utils.Timer;
	
	
	public class MainFlvPlayer extends FLVPlayback
	{
//		private var videoPath:String = "http://www.helpexamples.com/flash/video/sheep.flv"
		private var videoPath:String = "http://u2.7227.com/uploads/flash/138/2011102011102700000144618168.flv"
		private var offsetY:int=5	
		private var controlBar:ControlBar
		
		private var stateName:String
		
		private var tempVolume:Number
		private var tempState:String
		private var tempScaleNum:Number=1
			
		private var timer:Timer
		
		public var curPlayModel:PlayModel
		
		private var clickSp:Sprite
		
		private var curMouseX:int
		private var curMouseY:int
		
		private var index:int=0
		private var indexFrame:int=60
			
		private var parentX:int=0	
		private var parentY:int=0
		public function MainFlvPlayer(controlBar:ControlBar)
		{
			super();
			width=Data.playerWidth
			height=Data.playerHeight
			clickSp=new Sprite()
			drawSp()
			addChild(clickSp)
			
			this.controlBar=controlBar
			autoPlay=true
				
//			skinAutoHide=true 
//			skinScaleMaximum = 2.5;
			init()
//			source=videoPath
//			scaleMode=VideoScaleMode.MAINTAIN_ASPECT_RATIO
//			scaleMode=VideoScaleMode.NO_SCALE
				
			fullScreenTakeOver=false
			addEventListener(Event.ENTER_FRAME,onFrame)	
			addEventListener(Event.ADDED_TO_STAGE,addToSatge)	
			clickSp.addEventListener(MouseEvent.CLICK,doClick)
				
			/****************************/
//			var color:Number = 0xFF0000;
//			var alpha:Number = 0.8;
//			var blurX:Number = 5;
//			var blurY:Number = 5;
//			var strength:Number = 2;
//			var inner:Boolean = false;
//			var knockout:Boolean = false;
//			var quality:Number = BitmapFilterQuality.HIGH;
//			
//			filters=[new GlowFilter(color,
//				alpha,
//				blurX,
//				blurY,
//				strength,
//				quality,
//				inner,
//				knockout)]
			/****************************/
			
			if(as3_player.mode == as3_player.SWF_STAGE ){
				scaleMode=VideoScaleMode.EXACT_FIT 
			}
			
		}
		
		public function drawSp():void{
			clickSp.graphics.clear()
			clickSp.graphics.beginFill(0xffffff,0)
			clickSp.graphics.drawRect(0,0,registrationWidth,registrationHeight)
			clickSp.graphics.endFill()
				
			clickSp.x=registrationX
			clickSp.y=registrationY
		}
		
		public function doClick(e:Event):void{
			if(this.state=="playing"){
			   this.pause()
			}else if(this.state=="paused"){
				this.play()
			}
			if(curPlayModel && curPlayModel.name=="ad"){
				if (Capabilities.playerType == 'ActiveX')
				{
					ExternalInterface.call("window.open",curPlayModel.adUrl);
				}
				else
				{
					navigateToURL(new URLRequest(curPlayModel.adUrl),"_blank");
				}
			}
		}
		
		public function addToSatge(e:Event):void{
			if(as3_player.mode != as3_player.SWF_STAGE ){
				stage.addEventListener(Event.RESIZE,displayStateChange)	
			}
		}
		
		public function init():void{
			playPauseButton=controlBar.playPauseButton
				
			bufferingBar=controlBar.bufferingBar
				
			seekBar=controlBar.seekBar
			muteButton=controlBar.muteButton
			volumeBar=controlBar.volumeBar
				
			fullScreenButton=controlBar.fullScreenButton
			
//			skin="MinimaFlatCustomColorPlayBackSeekCounterVolMuteFull.swf"
//			fullScreenButton.addEventListener(MouseEvent.CLICK,doFullScreen)
				
			controlBar.changeState("normal",stage,this)
			stateName="normal"
				
				
			this.width=Data.playerWidth
			this.height=Data.playerHeight
			stateName="normal"
			controlBar.changeState("normal",stage,this)
			tempScaleNum=1
			changeVolume(0)
			changeVolume(1)
			
			setScaleNum(tempScaleNum)
			drawSp()
				
		}
		
		public function playFlvByUrl(url:String):void{
			if(url.indexOf("flv") != -1 ){
				play(url)
			}
			if(url.indexOf("f4v") != -1 ){
				play(url)
			}
			setSize(Data.playerWidth,Data.playerHeight)
			
//			addEventListener(VideoEvent.READY,doReady)
		}
		
	    public function doReady(e:VideoEvent):void{
			var tempWidth:int=this.getVideoPlayer(0).videoWidth
			var tempHeight:int=this.getVideoPlayer(0).videoHeight
				
			Data.re=tempWidth/tempHeight
			if(Data.playerWidth > tempWidth && Data.playerHeight > tempHeight){	
//				setSize(tempWidth,tempHeight)
//				setSize(800,800)
				this.x=(Data.playerWidth-this.width)/2
				this.y=(Data.playerHeight-this.height)/2
			}
		}
		
		public function playPlayModel(playModel:PlayModel):void{
			curPlayModel=playModel
			playFlvByUrl(playModel.url)
			timer=new Timer(1000,int(playModel.time))	
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerComplete)
			timer.addEventListener(TimerEvent.TIMER,doTimer)
			timer.start()
		}
		
		public function doTimer(e:TimerEvent):void{
			dispatchEvent(new PlayerEvent(PlayerEvent.TIME))
		}
		
		public function timerComplete(e:TimerEvent):void{
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE,timerComplete)
			timer.removeEventListener(TimerEvent.TIMER,doTimer)
			timer.stop()
			timer=null
//			dispatchEvent(new VideoEvent(VideoEvent.COMPLETE))
		}
		
		
		public function displayStateChange(e:Event):void{

			if(this.stage.displayState=="normal"){
				this.width=Data.playerWidth
				this.height=Data.playerHeight
				stateName="normal"
				controlBar.changeState("normal",stage,this)
				tempScaleNum=1
				changeVolume(0)
				changeVolume(1)
			}else if(this.stage.displayState=="fullScreen"){
				this.width=stage.fullScreenWidth
				this.height=stage.fullScreenHeight	
				stateName="fullScreen"
				controlBar.changeState("fullScreen",stage,this)
//				seek(seekBarInterval)
				changeVolume(0)
				changeVolume(1)
				
			}
			
			setScaleNum(tempScaleNum)
			drawSp()
			dispatchEvent(new PlayerEvent(PlayerEvent.RESET,[this.stage.displayState,this]))
		}
		
		public function changeVolume(mode:int):void{
			if(mode==0){
				tempVolume=volume
				volume=0
			}
			if(mode==1){
				volume=tempVolume
				if(volume==0){
					volume=0.001
				}	
			}
			
		}
		
		public function onFrame(e:Event):void{
//			trace("width "+width)
			
//			if(this.stage.displayState=="normal"){
//			   this.width=Data.playerWidth
//			   this.height=Data.playerHeight-150
//			}
			
			if(this.stage.displayState=="fullScreen"){
				setMouseXY(stage.mouseX,stage.mouseY)
			}
			
			if(tempState != this.state){
				tempState=this.state
				dispatchEvent(new PlayerEvent(PlayerEvent.PLAYER_STATE,tempState))	
			}
			controlBar.updateTime(ncMgr.videoPlayer.playheadTime/ncMgr.videoPlayer.totalTime)
			controlBar.timeBar.text=format(ncMgr.videoPlayer.playheadTime)+" / "+format(ncMgr.videoPlayer.totalTime)
		}
		
		public function setMouseXY(x:int,y:int):void{
			if(x == curMouseX && y ==curMouseY){
				if(index<indexFrame){
					index++
				}else{
					controlBar.visible=false
				}
			}else{
				curMouseX=x
				curMouseY=y
				index=0
				controlBar.visible=true
			}
		}
		
		public function format(crrentTime:Number):String
		{
			var hour:int = Math.floor(crrentTime/3600);
			var minute:int= Math.floor(crrentTime/60) % 60;
			var second:int = Math.floor(crrentTime%60);
			var strSecond:String = second < 10 ? "0" + second.toString():second.toString();
			var strMinute:String = minute < 10 ? "0" + minute.toString():minute.toString();
			var strHour:String = hour < 10 ? "0" + hour.toString():hour.toString();
			return strHour+":"+strMinute+":"+strSecond
		}
		
		
		public function setScaleNum(num:Number):void{
			tempScaleNum=num
			switch(stateName)
			{
				case "normal":
				{
					registrationWidth=Data.playerWidth*num
					registrationHeight=Data.playerHeight*num
					registrationX=(Data.playerWidth-registrationWidth)/2
					registrationY=(Data.playerHeight-registrationHeight)/2
					
					break;
				}
				case "fullScreen":
				{
					registrationWidth=stage.fullScreenWidth*num
					registrationHeight=stage.fullScreenHeight*num
						
					registrationX=(stage.fullScreenWidth-registrationWidth)/2
					registrationY=(stage.fullScreenHeight-registrationHeight)/2
					break;
				}
					
				default:
				{
					break;
				}
			}
			
		}
		
		
		
	}
}